/****************************************************
	文件：ChainOfResponsibilityPattern.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/10 15:55:27
	功能：责任链模式
*****************************************************/

using UnityEngine;

namespace DesignPatterns
{
    public class ChainOfResponsibilityPattern : MonoBehaviour
    {
        private Level1 mLv1;
        private void Start()
        {
            //责任链模式：
            //接收到请求，将请求沿链式传递，直到被执行

            //eg：链式关卡制作
            mLv1 = new Level1();
            mLv1.SetNext(new Level2()).SetNext(new Level3()).SetNext(new LevelDefault());//关卡链式编辑
        }
        private void Update()
        {
            InputHandler();
            mLv1.Update();//开启关卡
        }
        private void InputHandler()
        {
            if (LevelMgr.Instance.Inputable == false) return;
            if (Input.GetKeyDown(KeyCode.K))
            {
                LevelMgr.Instance.CurrentLevelKillCount++;
                LevelMgr.Instance.TotalKillCount++;
                Debug.LogFormat("击杀数 + 1\n当前关卡击杀数：{0}；击杀总数：{1}", LevelMgr.Instance.CurrentLevelKillCount, LevelMgr.Instance.TotalKillCount);
            }
        }

        /// <summary>
        /// 关卡基类
        /// </summary>
        public abstract class LevelBase
        {
            public abstract void Enter();
            public abstract void Update();
            public abstract void Exit();
            public abstract LevelBase SetNext(LevelBase nextLevel);
        }
        /// <summary>
        /// 第一关
        /// </summary>
        public class Level1 : LevelBase
        {
            bool mIsFirstEnter = true;
            bool mIsFirstExit = true;
            private LevelBase mNextLevel;

            public override void Enter()
            {
                if (mIsFirstEnter == false) return;
                LevelMgr.Instance.Init();
                mIsFirstEnter = false;
                Debug.Log("进入第一关");
            }
            public override void Exit()
            {
                if (mIsFirstExit == false) return;
                mIsFirstExit = false;
                Debug.LogFormat("第一关通过\n当前关卡击杀数：{0}；击杀总数：{1}", LevelMgr.Instance.CurrentLevelKillCount, LevelMgr.Instance.TotalKillCount);
            }
            public override LevelBase SetNext(LevelBase nextLevel)
            {
                mNextLevel = nextLevel;
                return mNextLevel;
            }
            public override void Update()
            {
                Enter();
                if (LevelMgr.Instance.TotalKillCount >= 2)
                {
                    Exit();
                    if (mNextLevel != null)
                    {
                        mNextLevel.Update();//进入下一关
                    }
                }
            }
        }
        /// <summary>
        /// 第二关
        /// </summary>
        public class Level2 : LevelBase
        {
            bool mIsFirstEnter = true;
            bool mIsFirstExit = true;
            private LevelBase mNextLevel;

            public override void Enter()
            {
                if (mIsFirstEnter == false) return;
                LevelMgr.Instance.Reset();
                mIsFirstEnter = false;
                Debug.Log("进入第二关");
            }
            public override void Exit()
            {
                if (mIsFirstExit == false) return;
                mIsFirstExit = false;
                Debug.LogFormat("第二关通过\n当前关卡击杀数：{0}；击杀总数：{1}", LevelMgr.Instance.CurrentLevelKillCount, LevelMgr.Instance.TotalKillCount);
            }
            public override LevelBase SetNext(LevelBase nextLevel)
            {
                mNextLevel = nextLevel;
                return mNextLevel;
            }
            public override void Update()
            {
                Enter();
                if (LevelMgr.Instance.TotalKillCount >= 5)
                {
                    Exit();
                    if (mNextLevel != null)
                    {
                        mNextLevel.Update();//进入下一关
                    }
                }
            }
        }
        /// <summary>
        /// 第三关
        /// </summary>
        public class Level3 : LevelBase
        {
            bool mIsFirstEnter = true;
            bool mIsFirstExit = true;
            private LevelBase mNextLevel;

            public override void Enter()
            {
                if (mIsFirstEnter == false) return;
                LevelMgr.Instance.Reset();
                mIsFirstEnter = false;
                Debug.Log("进入第三关");
            }
            public override void Exit()
            {
                if (mIsFirstExit == false) return;
                mIsFirstExit = false;
                Debug.LogFormat("第三关通过\n当前关卡击杀数：{0}；击杀总数：{1}", LevelMgr.Instance.CurrentLevelKillCount, LevelMgr.Instance.TotalKillCount);
            }
            public override LevelBase SetNext(LevelBase nextLevel)
            {
                mNextLevel = nextLevel;
                return mNextLevel;
            }
            public override void Update()
            {
                Enter();
                if (LevelMgr.Instance.TotalKillCount >= 9)
                {
                    Exit();
                    if (mNextLevel != null)
                    {
                        mNextLevel.Update();//进入下一关
                    }
                }
            }
        }
        /// <summary>
        /// 尚未开发的关卡
        /// </summary>
        public class LevelDefault : LevelBase
        {
            bool mIsFirstEnter = true;
            private LevelBase mNextLevel;

            public override void Enter()
            {
                if (mIsFirstEnter == false) return;
                LevelMgr.Instance.Reset();
                LevelMgr.Instance.ForbidInput();
                mIsFirstEnter = false;
                Debug.Log("新关卡尚未开发，敬请期待！");
            }
            public override void Exit() { }
            public override LevelBase SetNext(LevelBase nextLevel)
            {
                mNextLevel = nextLevel;
                return mNextLevel;
            }
            public override void Update()
            {
                Enter();
            }
        }
    }

    /// <summary>
    /// 关卡管理器
    /// </summary>
    public class LevelMgr : Singleton<LevelMgr>
    {
        private bool mInputable;
        /// <summary>
        /// 可接受用户输入
        /// </summary>
        public bool Inputable => mInputable;
        /// <summary>
        /// 当前关卡击杀数量
        /// </summary>
        public int CurrentLevelKillCount { get; set; }
        /// <summary>
        /// 击杀总数
        /// </summary>
        public int TotalKillCount { get; set; }

        public void Init()
        {
            mInputable = true;
            CurrentLevelKillCount = 0;
            TotalKillCount = 0;
            Debug.Log("LevelMgr init done.");
        }
        public void Reset()
        {
            mInputable = true;
            CurrentLevelKillCount = 0;
            Debug.Log("LevelMgr reset done.");
        }
        /// <summary>
        /// 禁止用户输入
        /// </summary>
        public void ForbidInput()
        {
            mInputable = false;
        }
    }
}